package com.example.fridaykt.controller

import com.example.fridaykt.common.base.BaseController
import com.example.fridaykt.common.result.R
import com.example.fridaykt.model.SysRole
import com.example.fridaykt.model.delFlag
import com.example.fridaykt.model.roleId
import com.example.fridaykt.repository.SysRoleRepository
import com.example.fridaykt.service.SysRoleService
import com.example.fridaykt.vo.Page
import com.example.fridaykt.vo.RoleVO
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.babyfish.jimmer.sql.kt.ast.expression.valueIn
import org.springframework.web.bind.annotation.*

@RestController
@Tag(name = "角色管理接口")
class RoleController(
    private val roleService: SysRoleService,
    private val sysRoleRepository: SysRoleRepository,
) : BaseController() {
    @GetMapping("/role")
    @Operation(summary = "查询所有角色")
    fun getRoleList(
        @RequestParam(defaultValue = "1") current: Int,
        @RequestParam(defaultValue = "10") pageSize: Int
    ): R {
        val page: Page<SysRole> = roleService.getRoleList(current, pageSize)
        return R.success(page)
    }

    @PostMapping("/role")
    @Operation(summary = "添加角色")
    fun add(@RequestBody role: SysRole): R {
        sysRoleRepository.save(role)
        return toAjax(1)
    }

    @PutMapping("/role")
    @Operation(summary = "修改角色")
    fun update(@RequestBody role: SysRole): R {
        sysRoleRepository.update(role)
        return toAjax(1)
    }

    @DeleteMapping("/role")
    @Operation(summary = "批量删除角色")
    fun deleteBatch(@RequestBody vo: RoleVO): R {
        sysRoleRepository.sql.createUpdate(SysRole::class) {
            set(table.delFlag, "2")
            where(table.roleId valueIn vo.key!!.toList())
        }.execute()
        return toAjax(1)
    }
}
